package com.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mall.entity.UserAddress;
import com.mall.pojo.ResponseMessage;
import com.mall.service.IUserAddressService;
import com.mall.util.ResponseUtil;
import com.mall.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: liang
 * @time: 2024-01-17 18:10
 */
@RestController
@RequestMapping("/user/address")
@CrossOrigin
public class UserAddressController {

    private IUserAddressService addressService;
    //Using constructor injection:
    @Autowired
    public UserAddressController(@Qualifier("userAddressServiceImpl") IUserAddressService addressService) {
        this.addressService = addressService;
    }

    //查看所有收货地址
    @GetMapping("all/{userId}")
    public ResponseMessage getAllAddressByUserId(@PathVariable String userId) {
        if (StrUtil.isBlankIfStr(userId)) {
            return ResponseUtil.fail("数据传入失败");
        }
        List<UserAddress> userAddresses = addressService.queryAllUserAddressByUserId(userId);
        Map<String, Object> content = new HashMap<>();
        content.put("userAddresses", userAddresses);
        return ResponseUtil.successWithData(content);
    }

    //新增收货地址
    @PostMapping("")
    public ResponseMessage addAddress(@RequestBody UserAddress userAddress,HttpServletRequest request) {
        if (userAddress == null) {
            return ResponseUtil.fail("数据传入失败");
        }
        userAddress.setUserId(UserUtils.userId(request));
        UserAddress dbUserAddress = addressService.addAddress(userAddress);
        Map<String, Object> content = new HashMap<>();
        content.put("userAddress", dbUserAddress);
        return ResponseUtil.successWithData(content);
    }

    //修改收获地址
    @PutMapping("")
    public ResponseMessage modifyAddress(@RequestBody UserAddress userAddress) {
        if (userAddress == null || userAddress.getAddressId() == null) {
            return ResponseUtil.fail("数据传入失败");
        }
        UserAddress dbUserAddress = addressService.modifyUserAddress(userAddress);
        Map<String, Object> content = new HashMap<>();
        content.put("userAddress", dbUserAddress);
        return ResponseUtil.successWithData(content);
    }

    //删除收货地址
    @DeleteMapping("{userAddressId}")
    public ResponseMessage modifyAddress(@PathVariable String userAddressId) {
        if (userAddressId == null) {
            return ResponseUtil.fail("数据传入失败");
        }
        int id = 0;
        try {
             id = Integer.parseInt(userAddressId);
        }catch (NumberFormatException e){
            return ResponseUtil.fail("数据传入失败");
        }


        int i = addressService.removeAddress(id);
        if (i == 0) {
            return ResponseUtil.databaseError();
        }
        return ResponseUtil.success();
    }

    //查看收获地址
    @GetMapping
    public ResponseMessage getUserAddressByUserId(HttpServletRequest request) {
        List<UserAddress> userAddresses = addressService.queryUserAddress(UserUtils.userId(request));
        return ResponseMessage.success(userAddresses);
    }

}
